#! /bin/sh

name=ncurses
_snapshot=20250426
version=6.5.${_snapshot}
revision=1
tarball_url="https://github.com/ThomasDickey/ncurses-snapshots/archive/refs/tags/v6_5_${_snapshot}.tar.gz"
tarball_blake2b="a6161e5de4d84f275ddab1fba500b1d383c9f8ab60628ceb7f5875e76b53c36d2689754fbc51ca46d3a61fa675d5592d959a11abcda0d6daf34ea30d65b7bbf2"
imagedeps="build-essential ncurses-bin patchelf"
hostdeps="gcc pkg-config"
deps="core-libs"

prepare() {
    autotools_recursive_regen
}

configure() {
        cf_cv_func_nanosleep=yes \
    autotools_configure \
        --enable-widec \
        --enable-pc-files \
        --with-shared \
        --with-cxx-shared \
        --with-cxx-binding \
        --without-normal \
        --without-debug \
        --with-manpage-format=normal \
        --with-pkg-config-libdir=${prefix}/lib/pkgconfig \
        --with-termlib \
        --without-ada
}

build() {
    make -j${parallelism}
}

package() {
    make install DESTDIR="${dest_dir}"

    for lib in ncurses ncurses++ form panel menu tinfo; do
       rm -vf                    "${dest_dir}${prefix}"/lib/lib${lib}.so
       echo "INPUT(-l${lib}w)" > "${dest_dir}${prefix}"/lib/lib${lib}.so
       ln -sfv ${lib}w.pc        "${dest_dir}${prefix}"/lib/pkgconfig/${lib}.pc
       # Set library soname
       patchelf --set-soname lib${lib}w.so "${dest_dir}${prefix}"/lib/lib${lib}w.so
    done
    rm -vf                     "${dest_dir}${prefix}"/lib/libcursesw.so
    echo "INPUT(-lncursesw)" > "${dest_dir}${prefix}"/lib/libcursesw.so
    ln -sfv libncurses.so      "${dest_dir}${prefix}"/lib/libcurses.so

    # Remove static libraries
    rm -rf "${dest_dir}${prefix}"/lib/*.a

    post_package_strip
}
